package com.nuonuo.entity;

import java.util.Map;

/**
 * @author duan
 * @version 1.0
 * @date 2023/11/1 16:02
 */
public class RestResponse<T> {

    public static final String STATUS_SUCCESS = "0000";
    public static final String STATUS_ERROR = "9999";
    public static final String STATUS_MESSAGE = "未知错误！";
    /**
     * 状态码
     */
    private String status;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 数据信息
     */
    private T data;

    /**
     * 发票请求流水号
     */
    private T fpqqlsh;

    public RestResponse() {
    }

    private RestResponse(String status, String message) {
        this.status = status;
        this.message = message;
    }

    private RestResponse(String status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getFpqqlsh() {
        return fpqqlsh;
    }

    public void setFpqqlsh(T fpqqlsh) {
        this.fpqqlsh = fpqqlsh;
    }

    public static RestResponse success(String message) {
        return new RestResponse(STATUS_SUCCESS, message);
    }

    public static RestResponse success(String message, Object data) {
        return new RestResponse(STATUS_SUCCESS, message, data);
    }

    public static RestResponse success(String status, String message, Object data) {
        return new RestResponse(status, message, data);
    }

    public static RestResponse error(String status, String message) {
        return new RestResponse(status, message);
    }

    public static RestResponse buildSuccessObj(String describe, Object result)
    {
        RestResponse response = new RestResponse();
        response.setStatus(STATUS_SUCCESS);
        response.setMessage(describe);
        response.setData(result);
        return response;
    }

    public static RestResponse buildErrorObj(String code, String describe)
    {
        RestResponse response = new RestResponse();
        response.setStatus(code.replace("E",""));
        response.setMessage(describe);
        return response;
    }

    public static RestResponse buildObj(Map<String,String> map){
        if(map == null){
            return error(STATUS_ERROR,STATUS_MESSAGE);
        }else if("success".equals(map.get("result"))){
            return success(map.get("message"));
        }else {
            return error(STATUS_ERROR,map.get("message"));
        }
    }
}

